/*
 * AD8302.c
 *
 *  Created on: Jun 14, 2021
 *      Author: Rupert
 */

#include "AD9833.h"
#include "main.h"
#include "stm32f4xx_ll_gpio.h"
#include "stm32f4xx_ll_spi.h"

#define FCLK 25000000	//设置晶振频率
//#define RealFreDat    268435456.0/FCLK//总的公式为 Fout=（Fclk/2的28次方）*28位寄存器的值
double RealFreDat = 268435456.0/FCLK;


// 需要一读一写，不然可能出现写不进去的情况
uint8_t SPI3_ReadWriteByte(uint8_t dat)
{
	uint8_t Rxdata;
	LL_SPI_TransmitData8(SPI3, dat);
	while(!LL_SPI_IsActiveFlag_TXE(SPI3)){}

	uint8_t retry=0;
	while (LL_SPI_IsActiveFlag_RXNE(SPI3))//检查指定的SPI标志位设置与否:接受缓存非空标志位
	{
		retry++;
		if(retry>200)
			return 0;
	}
	Rxdata = LL_SPI_ReceiveData8(SPI3);
//  HAL_SPI_TransmitReceive(&SPI5_Handler, &dat, &Rxdata, 1, 1000);
	return Rxdata; // 返回收到的数据
}

/********往ad9833写******************************/
unsigned char AD9833_SPI_Write(unsigned char* data,
                        unsigned char bytesNumber)
{
	unsigned char i =0;
	unsigned char writeData[5]	= {0,0, 0, 0, 0};
	LL_GPIO_ResetOutputPin(AD9833_FSYNC_GPIO_Port, AD9833_FSYNC_Pin);
	for(i = 0;i < bytesNumber;i ++)
	{
		writeData[i] = data[i + 1];
	}
	for(i=0 ;i<bytesNumber ;i++)
	{
		SPI3_ReadWriteByte(writeData[i]);
		// while (!LL_SPI_IsActiveFlag_TXE(SPI3)){} //检查指定的SPI标志位设置与否:发送缓存空标志位
	}
	LL_GPIO_SetOutputPin(AD9833_FSYNC_GPIO_Port, AD9833_FSYNC_Pin);
	LL_mDelay(1);
	return i;
}

/*-----------------------------------------------------
	void AD9833_SetRegisterValue(unsigned short regValue)
	寄存器写入函数
	regValue：16位数据
-----------------------------------------------------*/
void AD9833_SetRegisterValue(unsigned short regValue)
{
	unsigned char data[5] = {0x03, 0x00, 0x00};

	data[1] = (unsigned char)((regValue & 0xFF00) >> 8);
	data[2] = (unsigned char)((regValue & 0x00FF) >> 0);
	AD9833_SPI_Write(data,2);
}

/*-----------------------------------------------------
	void AD9833_SetFrequency(unsigned short reg, float fout)
	相位寄存器写入函数
	reg：寄存器选项
	val：16位的相位数据，P=2π/2^16*val
-----------------------------------------------------*/
void AD9833_SetFrequency(unsigned short reg, float fout)
{
	unsigned short freqHi = reg;
	unsigned short freqLo = reg;
	unsigned long val=RealFreDat*fout;//F寄存器的值
	freqHi |= (val & 0xFFFC000) >> 14 ;
	freqLo |= (val & 0x3FFF);
	AD9833_SetRegisterValue(AD9833_B28);
	AD9833_SetRegisterValue(freqLo);
	AD9833_SetRegisterValue(freqHi);
}

/*-----------------------------------------------------
	void AD9833_SetFrequencyQuick(float fout,unsigned short type)
	应用函数
	fout：频率
	type：输出模式选项
-----------------------------------------------------*/
void AD9833_SetFrequencyQuick(float fout,unsigned short type)
{
	AD9833_SetFrequency(AD9833_REG_FREQ0, fout);
	AD9833_Setup(AD9833_FSEL0, AD9833_PSEL0, type);
}

unsigned char AD9833_Init(void)
{
    AD9833_SetRegisterValue(AD9833_REG_CMD | AD9833_RESET);
    return (1);
}

/*-----------------------------------------------------
    void AD9833_Setup(unsigned short freq,unsigned short phase,unsigned short type)
        功能函数
    freq：输出所需频率寄存器
    phase：输出所需相位寄存器
    type：输出模式
-----------------------------------------------------*/
void AD9833_Setup(unsigned short freq,
				  unsigned short phase,
			 	  unsigned short type)
{
	unsigned short val = 0;

	val = freq | phase | type;
	AD9833_SetRegisterValue(val);
}
